package cn.edu.xmu.datamining.tangzk.mralgos.secondarysort;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;

public class KeyPair implements WritableComparable<KeyPair> {

	private int id1;
	private Text id2 = new Text();

	public KeyPair() {

	}

	public KeyPair(int id1, String id2) {
		this.id1 = id1;
		this.id2.set(id2);
	}

	@Override
	public void readFields(DataInput in) throws IOException {
		this.id1 = in.readInt();
		this.id2.readFields(in);
	}

	@Override
	public void write(DataOutput out) throws IOException {
		out.writeInt(this.id1);
		this.id2.write(out);
	}

	@Override
	public int compareTo(KeyPair o) {
		if (this.id1 != o.id1) {
			return this.id1 - o.id1;
		} else {
			return this.id2.compareTo(o.id2);
		}
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof KeyPair)) {
			return false;
		}
		KeyPair pair2 = (KeyPair) obj;
		return this.id1 == pair2.id1 && this.id2.equals(pair2.id2);
	}

	@Override
	public int hashCode() {
		return this.id1 ^ this.id2.hashCode();
	}

	@Override
	public String toString() {
		return this.id1 + ":" + this.id2;
	}

	public int getId1() {
		return id1;
	}

	public void setId1(int id1) {
		this.id1 = id1;
	}

	public Text getId2() {
		return id2;
	}

	public void setId2(Text id2) {
		this.id2 = id2;
	}

}
